package com.yang.doctorrecruitment.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yang.doctorrecruitment.dao.UserResumeDAO;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import java.io.*;
import java.util.Map;

public class ExtraResumeAction extends ActionSupport {
    private File upload;
    private String uploadFileName;


    public String ExtraResume() throws IOException {
        InputStream is=new FileInputStream(getUpload());
        Map session= ActionContext.getContext().getSession();
        //到时候取出文件就是根据绝对路径+文件名来下载文件
        String pathString= "/userresume/";//获取项目工程的绝对路径
        System.out.println(pathString+"------------------");
        String fileType=uploadFileName.substring(uploadFileName.lastIndexOf("."));//获取文件类型
        String UserID=(String) session.get("UserID");
        String fileName=UserID;//UUID.randomUUID为随机生成36位识别码，防止文件名重复
        String newAbsolutePath=pathString+fileName+fileType;//新文件的绝对路径
        String FileName=fileName+fileType;
        System.out.println(newAbsolutePath);
        OutputStream os=new FileOutputStream(newAbsolutePath);
        byte buffer[]=new byte[1024];
        int count=0;
        while((count=is.read(buffer))>0){
            os.write(buffer,0,count);
        }
        os.close();
        is.close();
//文件保存成功以后还需要做一件事就是把路径存储到数据库中，
        boolean res= UserResumeDAO.updataExtra(UserID,FileName);
        return SUCCESS;
    }
    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }
}
